package org.lilystudio.smarty4j.statement.function;

import java.io.Writer;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.lilystudio.smarty4j.Context;
import org.lilystudio.smarty4j.Engine;
import org.lilystudio.smarty4j.Template;
import org.lilystudio.smarty4j.TemplateException;
import org.lilystudio.smarty4j.statement.LineFunction;
import org.lilystudio.smarty4j.statement.ParameterCharacter;

import com.flying.common.log.Logger;
import com.flying.common.util.Constants;
import com.flying.common.util.ServiceHelper;
import com.flying.common.util.Utils;
import com.flying.framework.data.Data;
import com.flying.framework.module.LocalModule;
import com.flying.framework.util.SmartyUtils;

/**
 * @author wanghaifeng
 * 
 */
public class $datagrid extends LineFunction {
	private final static Logger logger = Logger.getLogger($datagrid.class);

	private static ParameterCharacter[] definitions = {
			new ParameterCharacter(ParameterCharacter.STROBJECT, null, "id"),
			new ParameterCharacter(ParameterCharacter.STROBJECT, null, "datasource"),
			new ParameterCharacter(ParameterCharacter.STROBJECT, null, "template"),
			new ParameterCharacter(ParameterCharacter.STROBJECT, null, "moduleId"),
			new ParameterCharacter(ParameterCharacter.STROBJECT, null, "view"),
			new ParameterCharacter(ParameterCharacter.STROBJECT, null, "params")
		};

	
	public void execute(Context context, Writer writer, Object[] args)
			throws Exception {
		//
		String id 		= (String) args[0];
		String datasource 		= (String)args[1];
		String template = (String) args[2];
		String moduleId 	= (String) args[3];
		String form 	= (String) args[4];
		String params 	= (String)args[5];
		
		LocalModule module = (LocalModule) context.get(Constants.MODULE_UID);
		if(StringUtils.isEmpty(moduleId)) moduleId = module.getId();
		
		Data formData = ServiceHelper.invoke("pas", "FormService:findByModuleAndCode", new Data("module", moduleId,"code", form));
		context.set("_form", formData);
		Long formId = formData.getLong("formId");
		List<Data> fields = ServiceHelper.invoke("pas", "FieldService:findByFormId", new Data("form_id", formId)).get("rows");
		context.set("_fields", fields);
		context.set("_actions", ServiceHelper.invoke("pas", "ActionService:findByFormId", new Data("form_id", formId)).get("rows"));
		List<Data> conditions = Utils.newArrayList();
		fields.stream().filter(x -> 1 ==x.getInteger("condition", 0)).forEach(f -> {
			conditions.add(f);
		});
		if(!conditions.isEmpty()) {
			context.set("_conditions", conditions);
		}
		
		if(params != null) {
			String[] tmps = StringUtils.split(params, ";");
			for(String tmp: tmps) {
				if(tmp != null) {
					String[] kv = StringUtils.split(tmp, "=");
					if(kv.length == 2)
						context.set(kv[0].trim(), kv[1].trim());
				}
			}
		}
		
		if(StringUtils.isEmpty(template)) template = module.getModuleConfig().getConfig("template.datagrid");
		
		Engine engine = SmartyUtils.getEngine(module);

		context.set("id", id);
		if(StringUtils.isEmpty(datasource)) datasource = formData.getString("datasource");
		context.set("datasource", datasource);
		
		Template t;
		try {
			t = engine.getTemplate(template);
			t.merge(context, writer);
		} catch (TemplateException e) {
			logger.error(e);
		}
		
	}
	
	public ParameterCharacter[] getDefinitions() {
		return definitions;
	}
}
